package org.flexlancer.datavisualization
{
	
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import mx.containers.Canvas;
	import mx.controls.Alert;
	import mx.core.UIComponent;
	import mx.managers.PopUpManager;
	
	import org.flexlancer.Logger.Logger;
	import org.flexlancer.components.LinkDetails;
	import org.flexlancer.components.Node;
	import org.flexlancer.data.LinkData;
	import org.flexlancer.enums.AppVariables;
	import org.flexlancer.utils.StringUtils;

	public class LinkMap extends Canvas
	{
		/*
		variables
		*/
		public var links:Array = new Array();
		public var buttonArray:Array = new Array();
		public var myGraphic:UIComponent = new UIComponent();  
		
		public var btn_01:Node = null;
		public var btn_02:Node = null;
		private var undoFlag:Boolean = false;
		public var n:Number = 0;
		public var btnName:String;
		public var mouseUpButton:Node;

		/*
		methods 
		*/
		public function LinkMap():void{
			try{
				this.addChild(this.myGraphic);
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		
		[Embed(source="images/prosecution_enable.jpg")] 
		[Bindable] 
		private var imgProsecution:Class;

		public function AddItem(label:String,btn_x:Number=385,btn_y:Number=180,
								btn_width:Number=50,btn_height:Number=50,btn_Name:String=""):String {
			try {
					if(label != null && label != ""){
					
					if (btn_Name==""){
						this.n++;
						this.btnName = this.n.toString();	
					}else{
						this.btnName = btn_Name;
						n = Number(btnName);
					}
					
					var button:Node = new Node();
					button.name = this.btnName;
					button.x = btn_x;
					button.y = btn_y;
					
					button.label = label;
					
					button.Title = label;
					button.Source = AppVariables.URL_GET_THUMBNAIL + "?id=" + StringUtils.ClipBoard;//this.imgProsecution;
					
					button.addEventListener(MouseEvent.MOUSE_DOWN,StartBtnDrag);
					button.addEventListener(MouseEvent.MOUSE_MOVE,StartBtnDraging);
					button.addEventListener(MouseEvent.MOUSE_UP,StopBtnDrag);
					button.addEventListener(MouseEvent.CLICK,GetSelection);
					button.contextMenu = this.parentApplication.cm;
					
					this.addChild(button);
					
					this.buttonArray.push(button);
					}
				return this.n.toString();
			}catch(err:Error) {
				Logger.LogE(err);
			}
			return "NONE";
		}
		public function RemoveItem(ButtonName:String):Boolean {
			try {
					if (this.CheckAssociation(ButtonName) == false){
						
					
					
					var chd:Node = this.getChildByName(ButtonName) as Node;
					if ( chd != null ){
						
						this.removeChild(chd);
						return true;
						
					}	
						
						}else {
							Alert.show("Cannot Delete; please remove association");
						}
						
											
				}catch(err:Error){
					Logger.LogE(err);
				}
				return false;
		}
		public function GetSelection(event:MouseEvent):void {
			try {
				if(this.btn_01 == null){
					this.btn_01 = Node(event.currentTarget);	
					//this.btn_01.opaqueBackground = "0x222200";
					this.btn_01.selected = true;
					
				}else if (this.btn_02 == null) {
				
					this.btn_02 = Node(event.currentTarget);
					//this.btn_02.opaqueBackground = "0x222200";	
					this.btn_02.selected = true;
					
				}else {
					//this.btn_01.opaqueBackground = "0x00BB00";
					this.btn_01.selected = false;
					this.btn_01 = this.btn_02;
					this.btn_02 = Node(event.currentTarget);
					//this.btn_02.opaqueBackground = "0x222200";
					this.btn_02.selected = true;
				}
				
				
			}catch(err:Error) {
				Logger.LogE(err);
			}
		}
		public function StartBtnDrag(event:MouseEvent):void {
				try {
					var rect:Rectangle = new Rectangle(0,0,
					this.width-(Node(event.currentTarget).width),
					this.height-(Node(event.currentTarget).height));
					event.currentTarget.startDrag(false,rect);
					event.stopImmediatePropagation();
				}catch(err:Error){
					Logger.LogE(err);
				}
			}
			
			public function StopBtnDrag(event:MouseEvent):void {
				try {
					event.currentTarget.stopDrag();
					this.mouseUpButton = event.currentTarget as Node;
					//var SelectedBtn:Button;
					//SelectedBtn = Button(event.currentTarget);
					
					//Alert.show(SelectedBtn.name.toString());
					//this.SetPreviewBottonPos(SelectedBtn.x,SelectedBtn.y,SelectedBtn.name);
				}catch(err:Error){
					Logger.LogE(err);
				}
			}

		public function StartBtnDraging(event:MouseEvent):void {
			try {
				this.DrawAllLinks(); 
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		public function CheckAssociation(NodeName:String):Boolean {
			try{
				for (var i:Number =0; i<this.links.length; i++){
					
					if(NodeName == LinkData(this.links[i]).btn1.name ||
					NodeName == LinkData(this.links[i]).btn2.name)	
						return true;
						
						break;
					}
				
				
			}catch(err:Error) {
				Logger.LogE(err);
			}
			return false;
		}
		
		public function ADDLink(b1:Node,b2:Node,parmReason:String="",parmDescription:String="",parmType:String=""):void {
			try {
				
				
				if(b1 != null && b2 != null)
				{
				var insert:LinkData = new LinkData(b1,b2,parmReason,parmDescription,parmType);
			//	Alert.show(insert.reason.toString());
				this.links.push(insert);
				this.DrawAllLinks();
								
				this.btn_01.selected = false;
				this.btn_02.selected = false;

				this.btn_01 = null;
				this.btn_02 = null;
				
				}
				
			}catch(err:Error) {
				Logger.LogE(err);
			}
		}
		/////////////////////////////////////////////////////////////
		/////////////////////////////////////////////////////////////
		/* private function getLinkColor(parmType:String):Number{
			try{
				//var color:Number = 0;
				var color:uint = 0;
				switch(parmType.toLowerCase()){
					case LinkTypes.BELIEVED_RELIABLE:
						color = 0x009900;
					break;
					case LinkTypes.UNKNOWN_RELIABILITY:
						color = 0xFF6600;
					break;
					case LinkTypes.KNOWN_RELIABLE:
						color = 0x0000FF;
					break;
					case LinkTypes.BELIEVED_UNRELIABLE:
						color = 0xFFFF00;
					break;
					default:
						color = 0;
					break;					
				}
				return color;
			}catch(err:Error){
				Logger.LogE(err);
			}
			return 0x0066CC;
		} */
		/*
		/////////////////////////////////////////////////////////
		////////////////////////////////////////////////
		public function DrawAllLinks():void{
			try{
				this.myGraphic.graphics.clear();
				
				//for ( var i:Number = 0 ; i < this.LinkArray1.length ; i ++ )
				for ( var i:Number = 0 ; i < this.links.length ; i ++ )
				{
					var btn1:Node = Node(this.links[i].btn1);
					var btn2:Node = Node(this.links[i].btn2);
					
					this.myGraphic.graphics.lineStyle(5, this.getLinkColor(this.links[i].type), .75);
					this.myGraphic.graphics.moveTo(btn1.x+ btn1.width/2,btn1.y+ btn1.height/2);
					this.myGraphic.graphics.lineTo(btn2.x+ btn2.width/2,btn2.y+ btn2.height/2);
					this.myGraphic.addEventListener(MouseEvent.CLICK,LinkClicked);
					
				}
			}catch(err:Error){
				Logger.LogE(err);
			}
		}*/
		public function DrawAllLinks():void{
			try{
				while ( this.myGraphic.numChildren > 0 ) 
					this.myGraphic.removeChildAt(0);
				
				for each ( var singleLink:LinkData in this.links ){
					singleLink.addEventListener(MouseEvent.MOUSE_OVER,LinkMouseOver);
					singleLink.addEventListener(MouseEvent.MOUSE_OUT,LinkMouseOut);
				 	singleLink.DrawLine();
					this.myGraphic.addChild(singleLink);
				}
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		
		private var linkDetails:LinkDetails = null;
		
		public function LinkMouseOver(evt:MouseEvent):void {
			try {
				if ( this.linkDetails == null ){
					this.linkDetails = PopUpManager.createPopUp(this,LinkDetails) as LinkDetails;
				}

				this.linkDetails.x = this.mouseX + 10;
				this.linkDetails.y = this.mouseY + 10;

				var curr:LinkData = LinkData(evt.currentTarget);

				this.linkDetails.type = curr.type;
				this.linkDetails.reason = curr.reason;
				this.linkDetails.description = curr.description;
			}catch(err:Error){
				Logger.LogE(err);
			}
		} 

		public function LinkMouseOut(evt:MouseEvent):void {
			try {
				PopUpManager.removePopUp(this.linkDetails);
				this.linkDetails = null;
			}catch(err:Error){
				Logger.LogE(err);
			}
		} 

		public function LinkClicked(evt:MouseEvent):void {
			try {
				var curr:LinkData = LinkData(evt.currentTarget);
				//Alert.show("Hello Gi "+curr.type);
				
				var clink:LinkDetails = PopUpManager.createPopUp(this,LinkDetails,true) as LinkDetails;
				clink.type = curr.type;
				clink.reason = curr.reason;
				clink.description = curr.description;
				
				PopUpManager.centerPopUp(clink);
				
			}catch(err:Error){
				Logger.LogE(err);
			}
		} 
		
		
		
		public function RemoveLink(label1:String="",label2:String=""):void {
			try {
				var bt1_name:String = label1;
				var bt2_name:String = label2;
				
				for (var i:Number =0; i<this.links.length; i++){
					
					if((bt1_name == Node(this.links[i].btn1).name &&
					bt2_name == Node(this.links[i].btn2).name)|| 
					(bt1_name == Node(this.links[i].btn2).name &&
					bt2_name == Node(this.links[i].btn1).name)){
						this.links.splice(i,1);	
						break;
					}
				}
				
				this.DrawAllLinks();
				
			}catch(err:Error) {
				Logger.LogE(err);
			}
		}
		
		public function getXML():XML{
			try{
				var xmlObject:XML = new XML("<Nodes />");
				for each ( var singleNode:Node in this.buttonArray ){
					xmlObject.appendChild(singleNode.getXML());
				}
				return xmlObject;
			}catch(err:Error){
				throw err;
			}
			return null;
		}
		public function getLinksXML():XML{
			try{
				var xmlObject:XML = new XML("<Links />");
				for each ( var singleLink:LinkData in this.links ){
					xmlObject.appendChild(singleLink.getXML());
				}
				return xmlObject;
			}catch(err:Error){
				throw err;
			}
			return null;
		}
	}//class ends
}//package ends